package com.sketch.service;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sketch.commom.CodeMsg;
import com.sketch.commom.Result;
import com.sketch.commom.utils.HttpUtil;
import com.sketch.commom.utils.JwtUtil;
import com.sketch.entity.SysUser;
import com.sketch.entity.WebUser;
import com.sketch.mapper.SysUserMapper;
import com.sketch.mapper.WebUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SystemUserService {
    @Resource
    private WebUserMapper webUserMapper;


    public Result<SaTokenInfo> doWechatLogin(WebUser webUser, String iCode) {
        List<WebUser> sysUserList = webUserMapper.selectList(new LambdaQueryWrapper<WebUser>()
                .eq(WebUser::getOpenid, webUser.getOpenid()));

        WebUser wechatUser;
        // 用户表中未获取到用户信息,即走注册流程
        if (CollectionUtils.isEmpty(sysUserList) || sysUserList.size() == 0) {
            wechatUser = new WebUser();
            wechatUser.setAvatar(webUser.getAvatar());
            wechatUser.setLoginIp(HttpUtil.getIp());
            wechatUser.setStatus('0');
            wechatUser.setLoginDate(new Date());
            wechatUser.setMemberType(1);
            wechatUser.setWithdrawGold(0);
            wechatUser.setUnionId(webUser.getUnionId());
            wechatUser.setUsername(webUser.getUsername());
            wechatUser.setAvatar(webUser.getAvatar());
            while (true) {
                String code = RandomUtil.randomString(7);
                WebUser count = webUserMapper.selectOne(new LambdaQueryWrapper<WebUser>().eq(WebUser::getInviteCode, code));
                if (count == null) {
                    wechatUser.setInviteCode(code);
                    break;
                }
            }
            Integer top = 0;
            if (StrUtil.isNotBlank(iCode)) {
                WebUser webUser1 = webUserMapper.selectOne(new LambdaQueryWrapper<WebUser>().eq(WebUser::getInviteCode, iCode));
                if (webUser1 != null) {
                    top = webUser1.getUserId();
                }
            }
            wechatUser.setTopId(top);

            webUserMapper.insert(wechatUser);
        } else { // 用户已注册,登录
            wechatUser = sysUserList.get(0);
            if (!"0".equals(wechatUser.getStatus())) {
//                log.error("冻结账号登录" + userModel.getId() + "  | " + userModel.getNickname());
                return Result.error(CodeMsg.FAIL);
            }
            //更新登录时间
            wechatUser.setLoginIp(HttpUtil.getIp());
            wechatUser.setLoginDate(new Date());
            webUserMapper.updateById(wechatUser);
        }
        StpUtil.login(wechatUser.getUserId(), SaLoginConfig.setExtra("user", wechatUser)
                .setExtra("name", wechatUser.getUsername()));
        return Result.success(StpUtil.getTokenInfo());
    }
}
